layui.use(['layer', 'form', 'admin', 'ax', 'dict', 'laydate', 'upload', 'layedit', 'xmSelect'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var dict = layui.dict;
    var upload = layui.upload;//获取自定义模块
    var layedit = layui.layedit;
    var xmSelect = layui.xmSelect;
    var laydate = laydate;

    dict.renderDictAll(); //渲染

    //table的初始化实例
    //获取详细信息
    var ajax = new $ax(Feng.ctxPath + "/erya/dish/dishDetail/" + decodeURI(escape(Feng.getUrlParam("id"))));
    var result = ajax.start();
    form.val('costingFrom', result);
    //重新渲染select数据
    form.render('select');
    $('#dishCode').val(result.data.dishCode);
    $('#dishName').val(result.data.dishName);
    $('#dishMaterialList').val(result.data.dishMaterialList);
    $('#id').val(result.data.id);



    // 表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        data.field.dishCode = $('#dishCode').val();
        data.field.dishName = $('#dishName').val();
        var selectDishArray = selectDish.getValue()
        var dishMaterialList = '';
        for(var i = 0; i < selectDishArray.length; i++) {
            dishMaterialList = (dishMaterialList + selectDishArray[i].value + ';');
        }
        data.field.dishMaterialList = dishMaterialList;
        data.field.id = $('#id').val();

        layer.load(0, {
            shade: [0.3, '#393D49'],
        }); //上传loading
        $.ajax({
            url: Feng.ctxPath + "/erya/dish/editData",
            type: "post", // 请求类型
            data: data.field,
            dataType: 'json',
            async: true, // 是否异步
            success: function (data) {
                if(data.code===200){
                    Feng.success("修改成功！");
                    //传给上个页面，刷新table用
                    admin.putTempData('formOk', true);
                    //关掉对话框
                    admin.closeThisDialog();
                }else {
                    layer.open({title: '失败', content: ''+data.message});
                    layer.closeAll('loading'); //关闭loading
                }
            },
            error: function (data) {
                layer.closeAll('loading');
                Feng.error("修改失败！" + data.responseJSON.message)
            }
        });

        //添加 return false 可成功跳转页面
        return false;
    });

    $.ajax({
        url: Feng.ctxPath + "/erya/rawmaterialdictionary/listAdd", // 替换为你的API端点
        type: 'POST',
        dataType: 'json',
        success: function(data){
            if (data.data.length === 0) {
                $('#dishMaterialList').html('请先添加食材名称');
                return;
            }
            var dataList = [];
            // 循环数据，构建下拉选项
            for(var i = 0; i < data.data.length; i++) {
                dataList.push({name: data.data[i].name, value: data.data[i].code});
            }
            selectDish.update({
                data: dataList
            });
        }
    });

    $('#addMaterial').click(function () {
        func.open({
            height: 650,
            title: '添加',
            content: Feng.ctxPath + '/erya/rawmaterialdictionary/rawmaterialdictionaryAdd',
            endCallback: function () {
                table.reload(Menu.tableId);
            }
        });
    });

    var selectDish = xmSelect.render({
        el: '#dishMaterialList',
        name: 'dishMaterialList',
        layVerify: 'required',
        data: []
    });

});